package com.auoem.radio.ui.msg;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.auoem.radio.R;
import com.auoem.radio.base.BaseActivity;
import com.auoem.radio.common.adapter.BindingAdapter;
import com.auoem.radio.common.custview.CustomTitlebar;
import com.auoem.radio.databinding.ActivityMsgListBinding;
import com.auctus.am.framework.constants.AmConstants;
import com.auctus.am.framework.imp.HlThreadPoolManager;
import com.auctus.am.framework.imp.MsgControl;
import com.auctus.am.framework.imp.RadioManager;
import com.auctus.am.framework.roomdb.entity.AmMsg;
import com.auctus.am.framework.roomdb.entity.AmTalk;
import com.auctus.am.framework.utils.LogM;

import java.util.ArrayList;
import java.util.List;

public class MsgListActivity extends BaseActivity implements View.OnClickListener, MsgControl.MsgListener {

    private static final String TAG = "MsgListActivity";
    ActivityMsgListBinding mBinding;

    private List<AmMsg> msgList;
    private BindingAdapter<AmMsg> msgAdapter;

    private int tarId;

    private MsgControl msgControl;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivityMsgListBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());
        initData();
        initView();
    }

    @Override
    protected void onStart() {
        super.onStart();
        requestAndUpdateMsgList();
    }

    private void initView() {
//        msgList = AmMsgRep.getInstance().getMsgByTarId(tarId);
        msgList = new ArrayList<>();
        msgAdapter = new BindingAdapter<>(this, msgList, R.layout.adapter_item_msg);
        mBinding.lvMsg.setLayoutManager(new LinearLayoutManager(this));
        mBinding.lvMsg.setAdapter(msgAdapter);
        initActionBar();
    }

    private void initActionBar() {
        ActionBar actionBar = getSupportActionBar();
        assert actionBar != null;
        actionBar.hide();
        mBinding.ctMsgList.setButtonOnClickCallback(new CustomTitlebar.ButtonCallback() {
            @Override
            public void OnClickResultCallback(CustomTitlebar.ButtonType btnType, View view) {
                switch (btnType) {
                    case LeftimgBtn:
                        finish();
                        break;
                }
            }
        });
    }

    private void initData() {
        Intent intent = getIntent();
        tarId = intent.getIntExtra(AmConstants.KEY_MSG_TAR_ID, 0);
        msgControl = RadioManager.getInstance().getMsgControl();
        msgControl.setMsgListener(this);
    }

    @Override
    protected void onDestroy() {
        msgControl.setMsgListener(null);
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        if (v == mBinding.btSend) {
            doSendMsg();
        }
    }

    private void doSendMsg() {

    }


    @Override
    public void onNewMsg(AmTalk talk, AmMsg msg) {
        LogM.d(TAG, "onNewMsg: " + talk.toString());
        requestAndUpdateMsgList();
    }

    private void requestAndUpdateMsgList() {
        HlThreadPoolManager.getInstance().execut("requestTalkDb", new Runnable() {
            @Override
            public void run() {
                List<AmMsg> newMsgs = msgControl.getMsgs();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //todo  update titlebar
                        msgList.clear();
                        msgList.addAll(newMsgs);
                        msgAdapter.notifyDataSetChanged();
                    }
                });
            }
        });
    }


}
